<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Controller', 'Controller');

//bring in the constants
include_once '../Config/constants.php';

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 */
class AppController extends Controller {
    
    // components available in all controllers
    var $components = array('Auth','RequestHandler','Session','Security');
    
    // helpers avaialable in all controllers
    var $helpers = array('Html','Js','Form','Session');
    
    // models that are always available
    var $uses = array('User','Role','Right');
    
    // define universal date format
    var $dateFormat = 'Y-m-d H:i:s';
   
    // set controller domain
    var $controllerDomain = null;
    
    function beforeFilter() {
        
        $this->layout = 'belagio';
        
        //$this->Security->csrfUseOnce  = false;
        //$this->Security->csrfExpires  = '+1 hour';
        
        $this->Auth->authorize = 'controller';
        $this->Auth->loginAction = '/';
        //$this->Auth->userScope = array('User.is_active' => 1);
        
        $this->Auth->loginAction = array('controller' => 'users','action' => 'login');
        $this->Auth->loginError = "Invalid username or password";
        $this->Auth->authError = "Invalid request";
        $this->Auth->loginRedirect = array('controller' => 'dashboard','action' => 'index');
        $this->Auth->logoutRedirect = array('controller' => 'users','action' => 'login');
        
        $this->Auth->allow(array('login','logout'));
        
        $this->set('userInfo',$this->getUserInfo());
        $this->set('controllerDomain',$this->controllerDomain);
        
    }
    
    function isAuthorized() {
        
        // if user cannot be found
        if (!$this->Auth->user('id')) {
            //return false;
        }
        
        return true;
    }
    
    function getUserInfo() {
        
        $user = $this->Auth->user();
        if ($user != null) {
            $user['rights'] = $this->Role->getActiveRights($this,$user['role_id']);
        }
       
        return $user;
    }
    
    function setAjaxLayout() {
        $this->autoRender = false;
        $this->layout = false;
    }
    
}
